// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from '@/store'
import router from './router'
import '@/assets/css/cover.css'
import { Button, Field, RadioGroup, Radio, Uploader, Popup, DatetimePicker, CellGroup, Cell, Dialog, Toast, Swipe, SwipeItem, Divider } from 'vant'

const components = { Button, Field, RadioGroup, Radio, Uploader, Popup, DatetimePicker, CellGroup, Cell, Dialog, Toast, Swipe, SwipeItem, Divider }

Vue.config.productionTip = false

Object.keys(components).forEach(key => {
  Vue.use(components[key])
})

let info = localStorage.getItem('userInfo')
if (info) {
  store.commit('SET_AVATAR', JSON.parse(info).headimgurl)
}
if (navigator.userAgent.indexOf('iPhone') !== -1) {
  window.wxURL = window.location + ''
}

let whiteList = ['/', '/assessView', '/poster']
router.beforeEach((to, from, next) => {
  if (whiteList.indexOf(to.path) !== -1) {
    store.commit('SET_SHOWTITLE_STUDENT', to.meta.hasTitle)
    store.commit('SET_TITLE_STUDENT', to.meta.title)
    next()
  } else {
    let token = localStorage.getItem('token')
    let user = sessionStorage.getItem('user')
    if (token) {
      store.commit('SET_SHOWTITLE_STUDENT', to.meta.hasTitle)
      store.commit('SET_TITLE_STUDENT', to.meta.title)
      if (!user) {
        next('/myIndex')
      } else {
        next()
      }
    } else {
      localStorage.removeItem('userInfo')
      next('/')
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
